package com.food.restaurantpro.adapters;

import java.util.ArrayList;

import com.food.restaurantpro.R;
import com.food.restaurantpro.fragments.YourPreOrderFragment;
import com.food.restaurantpro.log.Log;
import com.food.restaurantpro.objects.Food;
import com.food.restaurantpro.objects.PreOrderObject;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;


public class YourPreOrderListViewAdapter extends ArrayAdapter<PreOrderObject> {
	private YourPreOrderFragment mYourPreOrderFragment;
	public ArrayList<PreOrderObject> mPreOrderObjectList = new ArrayList<PreOrderObject>();
	private LayoutInflater inflater;
	
	/*private int mTotalPayment;
	private TextView mTvTotalPayment;*/
	
	public YourPreOrderListViewAdapter(YourPreOrderFragment yourPreOrderFragment, ArrayList<PreOrderObject> preOrderObjectList) {
		super(yourPreOrderFragment.getActivity(), R.layout.your_pre_order_lv_item, R.id.tv_title, preOrderObjectList);
		this.mYourPreOrderFragment = yourPreOrderFragment;
		this.mPreOrderObjectList = preOrderObjectList;
		inflater = LayoutInflater.from(mYourPreOrderFragment.getActivity());
		
		/*mTvTotalPayment = tvTotalPayment;
		mTotalPayment = 0;
		
		for (int i = 0; i < mPreOrderObjectList.size(); i++) {
			ArrayList<Food> orderDetailList = mPreOrderObjectList.get(i).getOrderdetail();
			for(int j = 0; j < orderDetailList.size(); j++) {
				int price = Integer.parseInt(orderDetailList.get(j).getFoodPrice().trim());
				mTotalPayment = mTotalPayment
						+ (orderDetailList.get(j).getFoodAmount() * price);
			}
		}
		mTvTotalPayment.setText("Total: " + "$" + mTotalPayment);*/
	}

	@Override
	public int getCount() {
		return mPreOrderObjectList.size();
	}

	@Override
	public PreOrderObject getItem(int position) {
		return mPreOrderObjectList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View listView = convertView;
		YourPreOrderHolder holder = null;
		if (convertView == null) {
			holder = new YourPreOrderHolder();
			listView = inflater.inflate(R.layout.your_pre_order_lv_item, null);
			holder.tvTitle = (TextView) listView.findViewById(R.id.tv_title);
			holder.ll_Root = (LinearLayout) listView.findViewById(R.id.ll_Root);
			holder.ll_childview = (LinearLayout) listView.findViewById(R.id.ll_childview);
			holder.tvSatus = (TextView) listView.findViewById(R.id.tv_status);
			holder.btnEdit = (Button) listView.findViewById(R.id.btnEdit);
			holder.btnCancel = (Button) listView.findViewById(R.id.btnCancel);
			holder.btnShare = (Button) listView.findViewById(R.id.btnShare);
			holder.tvTime = (TextView) listView.findViewById(R.id.tv_time);
			holder.tvPlace = (TextView) listView.findViewById(R.id.tv_place);
			listView.setTag(holder);

		} else {
			listView = convertView;
			holder = (YourPreOrderHolder) listView.getTag();
		}
		final PreOrderObject preOrderobject = mPreOrderObjectList.get(position);
		holder.tvTitle.setText(mYourPreOrderFragment.getActivity().getString(R.string.complete_oder) + " " + Integer.toString(position + 1));
		holder.tvTime.setText(mYourPreOrderFragment.getActivity().getString(R.string.your_pre_oder_will_be_completed_exactly_at) + " " + preOrderobject.getPreorderTime() + " hours of " + preOrderobject.getPreorderDate());
		holder.tvPlace.setText("Please come to " + preOrderobject.getPreorderPlaceName() + " to receive.");
		//
		holder.ll_childview.removeAllViews();
		int height = 0;
		ArrayList<Food> orderDetailList = preOrderobject.getOrderdetail();
		if(orderDetailList != null) {
			int sizeorderDetailList = orderDetailList.size();
		    if (sizeorderDetailList > 0) {
		    	for (int i = 0; i < sizeorderDetailList; i++) {
		    		LinearLayout ll_ChildGroup = new LinearLayout(mYourPreOrderFragment.getActivity());
		    		LayoutParams llparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		    		ll_ChildGroup.setLayoutParams(llparams);
		    		ll_ChildGroup.setOrientation(LinearLayout.HORIZONTAL);
		    		LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		    		lparams.setMargins(20, 0, 0, 0);
		    		TextView tvName = new TextView(mYourPreOrderFragment.getActivity());
		    		tvName.setSingleLine(true);
		    		
		    		TextView tvAmount = new TextView(mYourPreOrderFragment.getActivity());
		    		
		    		String foodName = orderDetailList.get(i).getFoodName();
		    		
		    		
		    		tvName.setLayoutParams(lparams);
		    		tvName.setHeight(45);
		    		tvName.setGravity(Gravity.CENTER_VERTICAL);
		    		tvName.setTypeface(null, Typeface.BOLD);
		    		
		    		
		    		tvAmount.setLayoutParams(lparams);
		    		tvAmount.setHeight(45);
		    		tvAmount.setText("x " + Integer.toString(orderDetailList.get(i).getFoodAmount()));
		    		tvAmount.setGravity(Gravity.CENTER_VERTICAL);
		    		
		    		
		    		float a = holder.btnEdit.getTextSize();
		    		//hardcode, co thoi gian sua lai
		    		//Samsung s2 = 20, note 2 = 26, nexus = 27
		    		if(holder.btnEdit.getTextSize() >= 27 ) {
		    			if(foodName != null &&foodName.length() > 30) {
			    			foodName = foodName.substring(0, 30);
			    		}
		    			tvName.setTextSize(22);
		    			tvName.setText(foodName);
		    			tvAmount.setTextSize(18);
		    		} else {
		    			if(foodName != null &&foodName.length() > 23) {
			    			foodName = foodName.substring(0, 23);
			    		}
		    			tvName.setTextSize(16);
		    			tvName.setText(foodName);
		    			tvAmount.setTextSize(13);
		    		}
		    		
		    		
		    		
		    		
		    		
		    		ll_ChildGroup.addView(tvName);
		    		ll_ChildGroup.addView(tvAmount);
		    		holder.ll_childview.addView(ll_ChildGroup);
		    	}	
		    	//48 is height tvName (unit: px)
	    		height = 48*sizeorderDetailList;
			}
		}
		if (convertPixelsToDp(height, mYourPreOrderFragment.getActivity()) < 100) {
			height = (int) convertDpToPixel(100, mYourPreOrderFragment.getActivity());
		}
		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, height);	
		layoutParams.addRule(RelativeLayout.BELOW, R.id.gl_imgv_title);
		holder.ll_Root.setLayoutParams(layoutParams);
		//set height for ll_child view
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, height, (float) 3.0);	
		holder.ll_childview.setLayoutParams(params);
		LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(0, height, (float) 1.0);
		holder.tvSatus.setLayoutParams(params1);
		//
		holder.btnCancel.setTag(position);
		holder.btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int which = -1;
				Object obj =  v.getTag();
				if(obj instanceof Integer){
					which  = ((Integer)obj).intValue();
				}
				if (which > -1) {
					mYourPreOrderFragment.showDialogCancelPreOrder(which);					
				}
			}
		});
		
		holder.btnEdit.setTag(position);
		holder.btnEdit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int which = -1;
				Object obj =  v.getTag();
				if(obj instanceof Integer){
					which  = ((Integer)obj).intValue();
				}
				if (which > -1) {
					mYourPreOrderFragment.showDialogEditPreOrder(which);					
				}
			}
		});
		return listView;
	}

	public static float convertPixelsToDp(float px, Context context){
	    Resources resources = context.getResources();
	    DisplayMetrics metrics = resources.getDisplayMetrics();
	    float dp = px / (metrics.densityDpi / 160f);
	    return dp;
	}
	
	public static float convertDpToPixel(float dp, Context context){
	    Resources resources = context.getResources();
	    DisplayMetrics metrics = resources.getDisplayMetrics();
	    float px = dp * (metrics.densityDpi / 160f);
	    return px;
	}
	//
	private static class YourPreOrderHolder {
		public TextView tvTitle;
		public LinearLayout ll_Root;
		public LinearLayout ll_childview;
		public TextView tvSatus;
		public Button btnEdit;
		public Button btnCancel;
		public Button btnShare;
		public TextView tvTime;
		public TextView tvPlace;
	}
	
	public void clearMemoryCache() {
		if (mPreOrderObjectList != null) {
			mPreOrderObjectList.clear();
			mPreOrderObjectList = null;
		}
		inflater = null;
	}
}
